<?php

class Utils {

	public static function loadModels($obj, $modelNames)
	{
		foreach ($modelNames as $modelName) {
			$plugin = '';
			if(strpos($modelName, '.') !== false){
				list($plugin, $modelName) = explode('.', $modelName);
				$plugin .= '.';
			}
            if ( isset($obj->{$modelName}) ) {
                continue;
            }

            $obj->{$modelName} = ClassRegistry::init($plugin . $modelName, 'model');
        }
	}

	public static function loadComponents($obj, $componentNames)
	{
		$controller = $obj;
        if (!is_subclass_of($obj, 'Controller'))
        {
            App::import('Core', 'Controller');
            $controller =& new Controller();
        }

        foreach ($componentNames as $componentName)
        {
            $plugin = '';
			if(strpos($componentName, '.') !== false){
				list($plugin, $componentName) = explode('.', $componentName);
				$plugin .= '.';
			}
        	if ( isset($obj->{$componentName}) )
            {
                continue;
            }
            App::import('Component', $plugin . $componentName);
            $componentClass = $componentName . 'Component';
            $obj->{$componentName} = new $componentClass();

            if (method_exists($obj->{$componentName}, 'initialize')) {
                $obj->{$componentName}->initialize($controller);
            }
            if (method_exists($obj->$componentName, 'startup')) {
                $obj->{$componentName}->startup($controller);
            }
        }
	}
}